package it.trekker.dao.ejb;

import java.util.Hashtable;

import javax.naming.InitialContext;

import org.apache.log4j.Logger;
import it.trekker.dao.DAOFactory;
import it.trekker.dao.IExcursionDAO;
import it.trekker.dao.IExcursionCoordinateDAO;
import it.trekker.dao.IPhotoDAO;
import it.trekker.dao.IPoiDAO;
import it.trekker.dao.IUserDAO;
import it.trekker.prefetch.IExcursionDAOWithPrefetch;

/*
 ***** JBoss 7.1 *****
 * Lookup for stateless bean:
 * 		ejb:<app-name>/<module-name>/<distinct-name>/<bean-name>!<fully-qualified-classname-of-the-remote-interface>
 * Lookup for stateless bean:
 * 		ejb:<app-name>/<module-name>/<distinct-name>/<bean-name>!<fully-qualified-classname-of-the-remote-interface?stateful>
 */

public class EJB3DAOFactory extends DAOFactory {
	
	private static Logger logger = Logger.getLogger(EJB3DAOFactory.class.getSimpleName());
	 
	@SuppressWarnings("unchecked")
	private static InitialContext getInitialContext()
			throws Exception {
		//Hashtable props = getInitialContextProperties(hostPort);
		Hashtable props = getInitialContextProperties();
		return new InitialContext(props);
		
	}
	@SuppressWarnings("unchecked")
	private static Hashtable getInitialContextProperties() {
		Hashtable props = new Hashtable();
		
		// For JBoss 6.1
//		props.put("java.naming.factory.initial", "org.jnp.interfaces.NamingContextFactory");
//		props.put("java.naming.factory.url.pkgs", "org.jboss.naming:org.jnp.interfaces");
//		props.put("java.naming.provider.url", "127.0.0.1:1099");
		
		// For JBoss 7.1
//		props.put("java.naming.factory.initial", "org.jboss.as.naming.InitialContextFactory");
//		props.put("java.naming.factory.url.pkgs", "org.jboss.ejb.client.naming");
//		props.put("java.naming.provider.url", "127.0.0.1:1099");
		
		// For JBoss 7.1 remote client
		// The given user/password must be set by using the command bin/add-user.sh ﻿(or.bat).
//		props.put("java.naming.factory.initial", "org.jboss.naming.remote.client.InitialContextFactory");
//		props.put("java.naming.factory.url.pkgs", "org.jboss.ejb.client.naming");
//		props.put("java.naming.provider.url", "remote://127.0.0.1:4447");
//		props.put(Context.SECURITY_PRINCIPAL, "trekker");
//		props.put(Context.SECURITY_CREDENTIALS, "123");
		
//		props.put("java.naming.provider.url", new ServerInfo().getHostAddress() + ":1099");
		
		return props;
	}

	@Override
	public IExcursionDAO getExcursionDAO() {
		try {
			InitialContext context = getInitialContext();
			// JBoss 6.1
//			IExcursionDAO result = (IExcursionDAO)context.lookup("TrekkerServer/ExcursionDAOWithPrefetch/remote");
			// JBoss 7.1
			IExcursionDAO result = (IExcursionDAO)context.lookup("ejb:TrekkerServer/TrekkerServer.jar/EJB3ExcursionDAO!it.trekker.dao.IExcursionDAO");
			return result;
		} catch (Exception e) {
			logger.error("Error looking up EJB3ExcursionDAO",e);
		}
		return null;
	}
	
	@Override
	public IExcursionDAOWithPrefetch getExcursionDAOWithPrefetch() {
		try {
			InitialContext context = getInitialContext();
			// JBoss 6.1
//			IExcursionDAOWithPrefetch result = (IExcursionDAOWithPrefetch)context.lookup("TrekkerServer/ExcursionDAOWithPrefetch/remote");
			// JBoss 7.1
			// Stateless session bean
//			IExcursionDAOWithPrefetch result = (IExcursionDAOWithPrefetch)context.lookup("ejb:TrekkerServer/TrekkerServer.jar/ExcursionDAOWithPrefetch!it.trekker.prefetch.IExcursionDAOWithPrefetch");
			// Stateful session bean
			IExcursionDAOWithPrefetch result = (IExcursionDAOWithPrefetch)context.lookup("ejb:TrekkerServer/TrekkerServer.jar/ExcursionDAOWithPrefetch!it.trekker.prefetch.IExcursionDAOWithPrefetch?stateful");
//			IExcursionDAOWithPrefetch result = (IExcursionDAOWithPrefetch)context.lookup("java:global/TrekkerServer/TrekkerServer/ExcursionDAOWithPrefetch!it.trekker.prefetch.IExcursionDAOWithPrefetch?stateful");
			return result;
		} catch (Exception e) {
			logger.error("Error looking up EJB3ExcursionDAOWithPrefetch",e);
		}
		return null;
	}

	@Override
	public IExcursionCoordinateDAO getExcursionCoordinateDAO() {
		try {
			InitialContext context = getInitialContext();
			// JBoss 6.1
//			IGPSCoordinateDAO result = (IGPSCoordinateDAO)context.lookup("TrekkerServer/EJB3GPSCoordinateDAO/remote");
			// JBoss 7.1
			IExcursionCoordinateDAO result = (IExcursionCoordinateDAO)context.lookup("ejb:TrekkerServer/TrekkerServer.jar/EJB3ExcursionCoordinateDAO!it.trekker.dao.IExcursionCoordinateDAO");
			return result;
		} catch (Exception e) {
			logger.error("Error looking up EJB3ExcursionCoordinateDAO",e);
		}
		return null;
	}

	@Override
	public IPhotoDAO getPhotoDAO() {
		try {
			InitialContext context = getInitialContext();
			// JBoss 6.1
//			IPhotoDAO result = (IPhotoDAO)context.lookup("TrekkerServer/EJB3PhotoDAO/remote");
			// JBoss 7.1
			IPhotoDAO result = (IPhotoDAO)context.lookup("ejb:TrekkerServer/TrekkerServer.jar/EJB3PhotoDAO!it.trekker.dao.IPhotoDAO");
			return result;
		} catch (Exception e) {
			logger.error("Error looking up EJB3PhotoDAO",e);
		}
		return null;
	}

	@Override
	public IPoiDAO getPoiDAO() {
		try {
			InitialContext context = getInitialContext();
			// JBoss 6.1
//			IPoiDAO result = (IPoiDAO)context.lookup("TrekkerServer/EJB3PoiDAO/remote");
			// JBoss 7.1
			IPoiDAO result = (IPoiDAO)context.lookup("ejb:TrekkerServer/TrekkerServer.jar/EJB3PoiDAO!it.trekker.dao.IPoiDAO");
			return result;
		} catch (Exception e) {
			logger.error("Error looking up EJB3PoiDAO",e);
		}
		return null;
	}
	
	@Override
	public IUserDAO getUserDAO() {
		try {
			InitialContext context = getInitialContext();
			// JBoss 6.1
//			IUserDAO result = (IUserDAO)context.lookup("TrekkerServer/EJB3UserDAO/remote");
			// JBoss 7.1
			IUserDAO result = (IUserDAO)context.lookup("ejb:TrekkerServer/TrekkerServer.jar/EJB3UserDAO!it.trekker.dao.IUserDAO");
			return result;
		} catch (Exception e) {
			logger.error("Error looking up EJB3UserDAO",e);
		}
		return null;
	}

}
